rm(list = ls())


# Set Working Directory to Replication Folder
taskdir 		<- "~/Dropbox/final_hai_perlman_replication/"
inputdir 		<- paste0(taskdir, "input/processing/regressions_survey1/")


setwd(taskdir)
library(tidyverse)
library(lfe)
require(data.table)
library(stringi)
library(gtable)
library(gridExtra)
library(grid)
library(lattice)


# Define all DVs
dvs <- c('tax_support' ,'politician_prevent' ,'politician_understand' ,
		'politician_advocate' ,'politician_sympathy'
		,'responsibility_local' ,'responsibility_federal' ,
		'responsibility_international')



# Main Results - big plot with three dvs
load(paste0(inputdir, 'ols_survey1_controls.RData'))


toplot <- tibble(dv = NA, subset = NA, beta = NA, se = NA)
for (dv in dvs) {
	temp_regressions <- outlist[[dv]]
	mod1 <- temp_regressions[[1]]
	mod2 <- temp_regressions[[2]]
	mod3 <- temp_regressions[[3]]
	mod4 <- temp_regressions[[4]]

	temp <- tibble(dv = dv, 
				   subset = rep(c('All', 'Republicans', 'Democrats', 'Independents'),1),
				   politician = c(rep('Republican',2),rep('Democrat',2)),
				   beta = c(mod1$coef[1], mod2$coef[1], mod3$coef[1],mod4$coef[1]),
				   se = c(mod1$rse[1], mod2$rse[1], mod3$rse[1],mod4$rse[1])
				   )
	toplot <- bind_rows(toplot, temp)
}
toplot <-toplot%>%filter(!is.na(dv))%>%
				mutate(up = beta + 1.96*se,
					   down = beta-1.96*se,
					   up90 = beta + 1.64*se,
					   down90 = beta - 1.64*se)



# write in DV names
toplot<- toplot %>% mutate(
						question = case_when(
							dv =="politician_understand" ~ "has a good understanding of wildfires and their causes?",
							dv =="politician_sympathy" ~ "How sympathetic or unsympathetic did the politician seem towards those impacted?",
							dv =="politician_prevent" ~ "will work to prevent future wildfires?",
							dv =="politician_advocate" ~ "will be an effective advocate for federal disaster relief?",
							dv =="responsibility_international" ~ "The international community",
							dv =="responsibility_federal" ~ "The federal government",
							dv =="responsibility_local" ~ "Local/state government",
							dv =="tax_support" ~ "How likely would you be to support this new tax?",
							TRUE ~ as.character(NA)
						)
)
question_levels <- c("has a good understanding of wildfires and their causes?",
								   "will work to prevent future wildfires?",
								   "will be an effective advocate for federal disaster relief?",
								   "How sympathetic or unsympathetic did the politician seem towards those impacted?",
								   "How likely would you be to support this new tax?",
								   "Local/state government",
								   "The federal government",
								   "The international community")

toplot$question <- factor(toplot$question,
						levels = question_levels)

# get party variable in correct order
toplot$subset<- factor(toplot$subset,
											levels = c("Independents","Democrats",
																"Republicans","All"))

# One big plot with main 3 questions
q1<- ggplot(toplot %>% filter(dv %in%
											 c('politician_understand',
											   'politician_prevent',
											   'politician_advocate')) )+
	geom_vline(xintercept = 0, linetype = 'dashed')+
	geom_linerange(aes(y = subset, xmax = up90, xmin = down90,
								color = subset,
								linetype = "90% CI"), 
								size = 0.6, width = 0.2)+
	geom_linerange(aes(y = subset, xmax = up, xmin = down,
								color = subset,
								linetype = "95% CI"), 
								size = 0.6, width = 0.2)+
	scale_linetype_manual(name = 'CI',
						  values = c('solid','dotted'))+
	geom_point(aes(y = subset, x = beta,
								color = subset), size = 2)+
	geom_text(aes(y = subset, x = beta,
												color = subset, label = round(beta,2) ),
												size = 3,
											nudge_y = 0.3)+
	theme_bw()+
	facet_wrap(~question,ncol=1)+
	scale_color_manual(values = list('All' = 'black',
																	'Democrats' = 'steelblue3',
																  'Independents' = 'grey60',
																  'Republicans' = 'tomato3'))+
	ggtitle("How confident are you that the politician:")+
	xlab('Treatment Effect')
q1
ggsave(paste0(taskdir, 'output/regression_plots/ols_survey1_controls/survey1_results_main.png'),
				q1,
				width = 7,
				height = 6,
				dpi = 600)

# Level of Government Responsible
q2<- ggplot(toplot %>% filter(dv %in%c('responsibility_local' ,
										'responsibility_federal' ,
										'responsibility_international')) )+
	geom_vline(xintercept = 0, linetype = 'dashed')+
	geom_linerange(aes(y = subset, xmax = up90, xmin = down90,
								color = subset,
								linetype = "90% CI"), 
								size = 0.6, width = 0.2)+
	geom_linerange(aes(y = subset, xmax = up, xmin = down,
								color = subset,
								linetype = "95% CI"), 
								size = 0.6, width = 0.2)+
	scale_linetype_manual(name = 'CI',
						  values = c('solid','dotted'))+
	geom_point(aes(y = subset, x = beta,
								color = subset), size = 2)+
	geom_text(aes(y = subset, x = beta,
												color = subset, label = round(beta,2) ),
												size = 3,
											nudge_y = 0.3)+
	theme_bw()+
	facet_wrap(~question,ncol=1)+
	scale_color_manual(values = list('All' = 'black',
																	'Democrats' = 'steelblue3',
																  'Independents' = 'grey60',
																  'Republicans' = 'tomato3'))+
	ggtitle("Please rank the following actors based on who you believe\n should bear the most responsibility to fund efforts to prevent\n future wildfires:")+
	xlab('Treatment Effect')
q2
ggsave(paste0(taskdir, 'output/regression_plots/ols_survey1_controls/survey1_results_levelsofgovernment.png'),
				q2,
				width = 7,
				height = 6,
				dpi = 600)



# Sympathy
q3<- ggplot(toplot %>% filter(dv %in%
											 c('politician_sympathy')) )+
	geom_vline(xintercept = 0, linetype = 'dashed')+
	geom_linerange(aes(y = subset, xmax = up90, xmin = down90,
								color = subset,
								linetype = "90% CI"), 
								size = 0.6, width = 0.2)+
	geom_linerange(aes(y = subset, xmax = up, xmin = down,
								color = subset,
								linetype = "95% CI"), 
								size = 0.6, width = 0.2)+
	scale_linetype_manual(name = 'CI',
						  values = c('solid','dotted'))+
	geom_point(aes(y = subset, x = beta,
								color = subset), size = 2)+
	geom_text(aes(y = subset, x = beta,
												color = subset, label = round(beta,2) ),
												size = 3,
											nudge_y = 0.3)+
	theme_bw()+
	scale_color_manual(values = list('All' = 'black',
																	'Democrats' = 'steelblue3',
																  'Independents' = 'grey60',
																  'Republicans' = 'tomato3'))+
	ggtitle("How sympathetic or unsympathetic did the\n politician seem towards those impacted?")+
	xlab('Treatment Effect')
q3
ggsave(paste0(taskdir, 'output/regression_plots/ols_survey1_controls/survey1_resultspolitician_sympathy.png'),
				q3,
				width = 7,
				height = 2.66,
				dpi = 600)

# Tax Support
q4<- ggplot(toplot %>% filter(dv %in%
											 c('tax_support')) )+
	geom_vline(xintercept = 0, linetype = 'dashed')+
	geom_linerange(aes(y = subset, xmax = up90, xmin = down90,
								color = subset,
								linetype = "90% CI"), 
								size = 0.6, width = 0.2)+
	geom_linerange(aes(y = subset, xmax = up, xmin = down,
								color = subset,
								linetype = "95% CI"), 
								size = 0.6, width = 0.2)+
	scale_linetype_manual(name = 'CI',
						  values = c('solid','dotted'))+
	geom_point(aes(y = subset, x = beta,
								color = subset), size = 2)+
	geom_text(aes(y = subset, x = beta,
												color = subset, label = round(beta,2) ),
												size = 3,
											nudge_y = 0.3)+
	theme_bw()+
	facet_wrap(~question,ncol=1)+
	scale_color_manual(values = list('All' = 'black',
																	'Democrats' = 'steelblue3',
																  'Independents' = 'grey60',
																  'Republicans' = 'tomato3'))+
	ggtitle("The government is considering imposing an energy tax \nto protect against future wildfires and other natural \ndisasters. This tax is projected to increase the average American \nhousehold's energy bill by 10-20%. How likely \nwould you be to support this new tax?")+
	xlab('Treatment Effect')
q4

ggsave(paste0(taskdir, 'output/regression_plots/ols_survey1_controls/survey1_resultstax_support.png'),
				q3,
				width = 7,
				height = 2.66,
				dpi = 600)









































